var express = require('express');
const { get, addTatolType, deleteTatolType, amendTotalType, getChilType,addChild,selforTotal,deloneChild,resChildType,getAssType} = require("../api/totalType")
var router = express.Router();// 路由


// 获取商品类型数据  get类型
router.get('/', async function (req, res, next) {
    // 解构当前页和每页显示条数
    const { curPage, eachPage } = req.query;
    const data = await get({ curPage, eachPage });
    res.send(data);
});

// 新增商品总类型数据 post类型
router.post('/add-totalType', async function (req, res, next) {
    const totalTypeName = req.body;
    const data = await addTatolType(totalTypeName)
    res.send(data)
})



// 删除商品总类型
router.delete('/delete-totalType', async function (req, res, next) {
    const { _id } = req.body;

    await deleteTatolType({ _id })


})

//修改商品类型
router.put('/amend-totalType', async function (req, res, next) {
    const newType = req.body;
    await amendTotalType(newType)
    //返回状态值
    res.send({ satte: true })
})


//获取子类
router.get('/childType', async function (req, res, next) {
   const {curPage,
    eachPage}=req.query
    const data = await getChilType({curPage,
        eachPage})
 
    res.send(data)
})
// 新增子类
router.post("/addChild",async (req, res, next)=>{
    const {Father_id,childType_name}=req.body
    const data=await addChild({Father_id,childType_name})
    res.send(data)

})

// 查询子类  按照夫类型id
router.get("/selforTotal",async (req, res, next)=>{
    const {Father_id}=req.query
    const data=await selforTotal({Father_id})
    res.send(data)


})

// 删除子类
router.delete("/deloneChild",async (req, res, next)=>{
    const {_id}=req.query
    const data=await deloneChild(_id)
    res.send(data)

})


// 修改子类名字

router.patch("/resChildType",async (req, res, next)=>{
    const { childType_name,_id }=req.body
    const data=await resChildType({ childType_name,_id })
    res.send(data)
})

//获取总类型，子类型，关联类型数据

router.get("/assType",async (req, res, next)=>{
   const data= await getAssType()
   res.send(data)
    
})

module.exports = router;